﻿window.addEvent('domready', function () {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    checkShowPopUp();

});


function EndRequestHandler(sender, args) {
    checkShowPopUp();
}

function checkShowPopUp() {
    var HiddenComentList = $('MainContent_HiddenIdParticipante');
    if (HiddenComentList) {
        var id = HiddenComentList.get('value');
        if (id.length > 0) {
            showPopUp(id);
        }
    }
}

function showPopUp(id) {
    var divContentComentList = $('MainContent_TextComentario');
    if (divContentComentList) {
        divContentComentList.set('value', '');
    }
    var HiddenComentList = $('MainContent_HiddenIdParticipante');
    if (HiddenComentList) {
        HiddenComentList.set('value', id);
    }
    getComentList(id);
    var popUp = $('popUpComentarios');
    if (popUp) {
        popUp.setStyle('display', 'block');
        var scroll = window.getScroll();
        var popUpHeight = popUp.getSize().y;
        if (popUpHeight + 200 >= window.getSize().y) {
            popUp.set('tween', { duration: 'short' });
            popUp.tween('top', 15 + scroll.y);
        }
        else {
            popUp.setStyles({
                top: 200 + scroll.y
            });
        }
        popUp.setStyles({
            left: ((window.getCoordinates().width - popUp.getCoordinates().width) / 2)
        });
    }
}

function closePopUp() {
    var popUp = $('popUpComentarios');
    if (popUp) {
        popUp.setStyle('display', 'none');
        var HiddenComentList = $('MainContent_HiddenIdParticipante');
        if (HiddenComentList) {
            HiddenComentList.set('value', '');
        }

    }

}

function getComentList(id) {
    var divContentComentList = $('contentComentList');
    if (divContentComentList) {
        divContentComentList.set('html', 'Cargando comentarios...');
    }

    new Request.HTML({
        'url': '/HandlersHtml/GetComentList.ashx',
        'onSuccess': function (responseTree, responseElements, responseHTML, responseJavaScript) {
            var response = responseHTML;
            var divContentComentList = $('contentComentList');
            if (divContentComentList) {
                divContentComentList.set('html', response);
            }
        },
        'onFailure': function (reqObj) {
            alert('No se pudo obtener la lista de comentarios');
        }
    }).post({ 'id': id });
}